package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.service.IAgencySalaryService;
import com.boss.salary.service.IProvinceService;
import com.boss.salary.vo.AgencySalaryVO;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/provinceController")
public class ProvinceController extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(ExcelController.class);

    @Autowired
    private IAgencySalaryService service;

    @Autowired
    private IProvinceService provinceService;

    /**
     * 区划初始化数据导入
     * @return
     */
    @PostMapping("/provinceImportSalaryReportList")
    public ApiResultDTO provinceImportSalaryReportList(@RequestHeader("tokenid") String tokenid,
                                                       @RequestParam(value = "salaryMonth") String salaryMonth,
                                                       @RequestParam("file") MultipartFile file){
        try {
//            System.out.println("开始时间："+DateUtils.getCurrentTimeStamp());
            UserDTO userDTO = getUser(tokenid);
            List<AgencySalaryVO> exccelColumnName = service.getExccelColumnName(userDTO);
            String province = userDTO.getProvince();
            SimpleDateFormat sdf  = new SimpleDateFormat("yyyy-MM");
            Date date=sdf.parse(salaryMonth);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
            salaryMonth = sdf.format(calendar.getTime());
            provinceService.Read(file,province,exccelColumnName,salaryMonth);
            return ApiResultDTO.success();
        } catch (AppRuntimeException e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导入失败");
        }

    }

    /**
     * 区划数据初始化导出
     * @return
     */
    @GetMapping("/outPutProvinceReportList")
    public ApiResultDTO outPutProvinceReportList( @RequestHeader("tokenid") String tokenid,
                                                  HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);
            List<AgencySalaryVO> exccelColumnName = service.getExccelColumnName(userDTO);
            String fileName = "区划数据初始化";
            OutputStream outputStream = getOutputStream(fileName,response);
            provinceService.provinceWriteExcel03(exccelColumnName,outputStream,userDTO.getProvince());
            return ApiResultDTO.success();
        } catch (Exception e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }

    /**
     * 导出文件时为Writer生成OutputStream
     */
    static OutputStream getOutputStream(String fileName, HttpServletResponse response) throws AppRuntimeException{
        //创建本地文件
        fileName = fileName + ".xls";

        try {
            fileName = new String(fileName.getBytes(), "UTF-8");
            response.addHeader("Content-Disposition", "filename=" + fileName);

            return response.getOutputStream();
        } catch (Exception e) {

            throw new AppRuntimeException("下载失败");
        }
    }
}
